package cn.mill.core.modem;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.mill.common.Context;
import cn.mill.common.data.ItemArray;
import cn.mill.core.MillConfig;
import cn.mill.core.Result;

public class ResultSpell implements ObjectSpell {
	
	private static final Logger logger = LoggerFactory.getLogger(ResultSpell.class);
	
	public static ResultSpell INSTANCE = new ResultSpell();
	
	@SuppressWarnings("unchecked")
	@Override
	public <T> T builder(Type type, ItemArray itemArray) {
		Type subType = null;
		Result<Object> result = new Result<Object>();
		if (type != null && type instanceof ParameterizedType && itemArray != null) {
			logger.debug("ParameterizedType:[" + type + "]");
			subType = ((ParameterizedType) type).getActualTypeArguments()[0];
			ObjectSpell objectSpell = (ObjectSpell) MillConfig.getBuilder(subType);
			result.setValue(objectSpell.builder(subType, itemArray));
		}
		return ((T) result);
	}
	
	@Override
	public ItemArray resolver(Context context, Type type, Object object) {
		return null;
	}
}
